var loginid,loginuser,loginname,loginkey;
var isjump = 0;//控制翻页
var layer,laydate,upload,element,form,loadmsg,edite_storeid,isadd;
var jumpobj;//跳转对象（page_r 充值  page_c 消费）
var alertmsg = "";
var bigtext ="";//获取城市
var store_image ="";
layui.config({
	version:'1510164124181'
}).use(['form','laydate', 'layer', 'element','upload'],function(){
	form = layui.form;
	layer = layui.layer;
    laydate = layui.laydate;//日期
    element = layui.element; //元素操作
    upload = layui.upload;//上传
	loginid = $.session.get("userid");
	loginuser = $.session.get("admin_realname");
	loginname = $.session.get("username");
	loginkey = $.session.get("user_secretkey");
	edite_storeid = $.session.get("edite_storeid");//店铺id  为0是添加
	comm_big();
	//普通图片上传
    var uploadInst = upload.render({
		elem: '#uploadimgs'
		,url: '../../../../UploadServlet?serialtype=storeimages'
		,size: 66
		,before: function(obj){
		  //预读本地文件示例，不支持ie8
		  obj.preview(function(index, file, result){
			$("#divpreview").css("display","block");
			$('#preview').attr('src', result); //图片链接（base64）
		  });
		}
		,done: function(res){
			console.log("res====="+JSON.stringify(res));
		  //如果上传失败
		  if(res.code > 0){
			return layer.msg('上传失败');
		  }
		  store_image = res.data[0].src;
		}
		,error: function(){
		  //演示失败状态，并实现重传
			var demoText = $('#demoText');
			demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn  demo-reload">重试</a>');
			demoText.find('.demo-reload').on('click', function(){
			uploadInst.upload();
		  });
		}
	 });

});
//初始化数据
function initdatas(){
	var index = top.layer.msg('加载中，请稍候',{icon: 16,time:false,shade:0.8});
	$.ajax({
		url: "../../../../service",
		cache: false,
		async: false,
		data: {
			proc: "c_admin.store_selbyid",
			para: "['" + loginname + "','" + loginkey + "',"+(edite_storeid*1)+"]"
		},
		dataType: "json",
		success: function(data) {
			top.layer.closeAll();
			console.log("初始化数据==="+JSON.stringify(data));
			if(data[0].TAG=='0'){
				top.layer.alert(""+data[0].MSG+"",function(index){
				   top.location.href="login.html";
				   layer.close(index);
			    });
			}else if(data[0].TAG=='2'){
			   layer.msg(""+data[0].MSG+"");
			}else if(data[0].msg=='读取接口失败,参数错误'||data.msg=='读取接口失败,参数错误'){
			}else{
			   store_image = data[0].SHOP_URL;
			   $("#divpreview").css("display","block");
			   $('#preview').attr('src', '../../../../storeimages/'+store_image); //图片链接（base64）
			   $("#sname").val(data[0].SHOP_NAME);
			   $("#bigid").val(data[0].BIGID);
			   comm_smallinit(data[0].BIGID);
			   $("#smallid").val(data[0].SMALLID);
			   $("#saddr").val(data[0].STORE_ADDRESS);
			   $("#slitude").val(data[0].SHOP_LITUDE);
			   $("#stime").val(data[0].START_TIME);
			   $("#stel").val(data[0].SHOP_PHONE);
			   var saddr = $("#saddr").val();
			   likemap(saddr);
			}

		}
	});
}
//查询大地区
function comm_big(){
	$.ajax({
		url: "../../../../service",
		cache: false,
		async: false,
		data: {
			proc: "c_admin.community_selall_big",
			para: "['" + loginname + "','" + loginkey + "']"
		},
		dataType: "json",
		success: function(data) {
			//console.log("查询大地区==="+JSON.stringify(data));
			var selecthtml='<option value="0">请选择城市</option>';
			if(data[0].TAG=='0'){
				top.layer.alert(""+data[0].MSG+"",function(index){
				   top.location.href="login.html";
				   layer.close(index);
			    });
			}else if(data[0].TAG=='2'){
			}else if(data[0].msg=='读取接口失败,参数错误'||data.msg=='读取接口失败,参数错误'){
			}else{
			   for(var i=0;i<=data.length-1;i++){
				   selecthtml+='<option value="'+data[i].ID+'">'+data[i].CNAME+'</option>';
			   }
			}
			$("#bigid").html(selecthtml);
			if(edite_storeid ==0){
				alertmsg = "添加此店铺";
			}else{
				alertmsg = "更改此店铺";
				initdatas();
			}
		}
	});
}
//根据选择的城市查询出小地区，并定位到所在的城市
function comm_small(obj){
    bigtext = $(obj).find("option:selected").text();
	var bigid = $(obj).val();
	if(bigid == '0'){
		return;
	}
	comm_smallinit(bigid);
}
//根据选择的城市查询出小地区，并定位到所在的城市()
function comm_smallinit(obj){
	$.ajax({
		url: "../../../../service",
		cache: false,
		async: false,
		data: {
			proc: "c_admin.community_selall_small",
			para: "['" + loginname + "','" + loginkey + "','" + (obj*1) + "']"
		},
		dataType: "json",
		success: function(data) {
			console.log("查询出小地区==="+JSON.stringify(data));
			var selecthtml='<option value="0">请选择地区</option>';
			if(data[0].TAG=='0'){
				top.layer.alert(""+data[0].MSG+"",function(index){
				   top.location.href="login.html";
				   layer.close(index);
				});
			}else if(data[0].TAG=='2'){
			}else if(data[0].msg=='读取接口失败,参数错误'||data.msg=='读取接口失败,参数错误'){
			}else{
			   for(var i=0;i<=data.length-1;i++){
				   selecthtml+='<option value="'+data[i].ID+'">'+data[i].CNAME+'</option>';
			   }
			}
			$("#smallid").html(selecthtml);
			if(edite_storeid ==0){
				likemap(bigtext);
			}
		}
	});
}
//根据选择的地区定位到所在的位置
function getcomm_small(obj){
    var smalltext = $(obj).find("option:selected").text();
	var smallid = $(obj).val();
	if(smallid == '0'){
		return;
	}
	var likeobj=bigtext+""+smalltext;
	likemap(likeobj);
}
//根据选择的城市或者地区模糊搜索
function likemap(obj){
    $("#container").css("display","block");
	AMap.service(["AMap.PlaceSearch"], function() {
		var placeSearch = new AMap.PlaceSearch({ //构造地点查询类
			pageSize: 5,
			pageIndex: 1,
			city: "010", //城市
			map: map//,
			//panel: "panel"
		});
		//关键字查询
		placeSearch.search(''+obj+'', function(status, result) {

		});
	});

}
//修改或者添加地区
function update(){
	if(store_image=="" || store_image==null || store_image==undefined){
		layer.msg("请上传店铺图片！",{icon:2});
		return;
	}
	var sname = $("#sname").val().replace(/(^\s+)|(\s+$)/g, "");
	if(sname.length==0){
		layer.msg("商品名称至少一个字符！",{icon:2});
		$("#sname").focus();return;
	}
	var smallid = $("#smallid").val();
	if(smallid=='0'){
        layer.msg("请选择地区！",{icon:2});
		$("#smallid").focus();return;
	}
	var smalltext = $("#smallid").find("option:selected").text();
	var saddr = $("#saddr").val().replace(/(^\s+)|(\s+$)/g, "");
	if(saddr=='' || saddr==null){
        layer.msg("请选择地址！",{icon:2});
		$("#saddr").focus();return;
	}
	if(saddr.indexOf(""+smalltext+"") <= 0 ){
		 layer.msg("选择的地址与地区不匹配！",{icon:2});
		$("#saddr").val("");
		$("#saddr").focus();return;
	}
	var slitude = $("#slitude").val();
	if(slitude==""){
        layer.msg("选择的地址与地区不匹配！",{icon:2});
		$("#slitude").val("");
		$("#slitude").focus();return;
	}
	var stime = $("#stime").val();
	if(stime==""){
        layer.msg("请输入营业时间",{icon:2});
		$("#stime").focus();return;
	}
	var tel = $("#stel").val();
	var regPartton=/1[3-8]+\d{9}/;
	if(!tel || tel==null || !regPartton.test(tel)){
		layer.msg("手机号码格式不正确！",{icon:2});
		$("#stel").focus();return;
	}
    top.layer.confirm('您确定要'+alertmsg+'吗？',{icon: 3,title:'提示信息'},function(index){
		var index = top.layer.msg('加载中，请稍候',{icon: 16,time:false,shade:0.8});
		setTimeout(function(){
			$.ajax({
				url: "../../../../service",
				cache: false,
				data: {
					proc: "c_admin.store_ediet",
					para: "['" + loginname + "','" + loginkey + "',"+edite_storeid+",'"+store_image+"','"+sname+"','"+smallid+"','"+saddr+"','"+slitude+"','"+stime+"','"+tel+"']"
				},
				dataType: "json",
				success: function(data) {
					console.log("====="+JSON.stringify(data));
					if(data[0].TAG=='0'){
						top.layer.closeAll();
						top.layer.alert(""+data[0].MSG+"",function(index){
						   top.location.href="login.html";
						   layer.close(index);
					    });
					}else if(data[0].msg=='读取接口失败,参数错误'||data.msg=='读取接口失败,参数错误'){
						top.layer.closeAll();
						top.layer.msg("修改失败，请检查您的网络环境!");
						console.log("读取接口失败,参数错误");
					}else if(data[0].TAG == 2){
						top.layer.msg(""+data[0].MSG+"");
					}else{
						top.layer.closeAll();
						top.layer.msg(""+data[0].MSG+"");
						returnlist();
					}
				}
			});
		},1000);
	})
}
//返回
function returnlist(){
	$.session.remove('edite_storeid');
	window.location.href="shop_list.html";
}
